package com.tss.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tss.pojo.Answer;
import com.tss.pojo.Optionen;
import com.tss.pojo.Question;
import com.tss.pojo.Result;
import com.tss.service.AnswerService;
import com.tss.service.OptionenService;
import com.tss.service.QuestionService;
import com.tss.vo.Dictionary;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("answer")
public class AnswerController {

	@Autowired
	private AnswerService answerService;
	
	@Autowired
	private QuestionService questionService;
	
	@Autowired
	private OptionenService optionenService;
	
	/**
	 * 提交问卷答案
	 * @param answers
	 * @param questionnaireId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("submit")
	public String submit(String answers,Long questionnaireId){
		Result result = new Result();
		if(answers==null||questionnaireId==null){
			return result.setErrorMessage("参数错误！").toString();
		}
		List<Question> questions = questionService.listQuestion(questionnaireId);
		if(questions==null||questions.isEmpty()){
			return result.setErrorMessage("问卷不存在！").toString();
		}
		//解析JSON
		try{
			JSONObject answersJson = JSONObject.fromObject(answers);
			for(Question question:questions){
				Answer answer = new Answer();
				answer.setQuestionId(question.getId());
				if(question.getType()==Dictionary.QUESTION_TYPE_OBJECTIVE){
					answer.setContent(answersJson.getString(question.getId().toString()));
					answerService.addAnswer(answer);
				}else if(question.getType()==Dictionary.QUESTION_TYPE_SINGLECHOICE){
					String content = answersJson.getString(question.getId().toString());
					answer.setContent(content);
					answer.setOptionenId(getOptionenId(content,question.getId()));
					answerService.addAnswer(answer);
				}else if(question.getType()==Dictionary.QUESTION_TYPE_MULTIPLECHOICE){
					JSONArray array = answersJson.getJSONArray(question.getId().toString());
					List<String> list = JSONArray.toList(array);
					for(String str:list){
						answer.setContent(str);
						answer.setOptionenId(getOptionenId(str,question.getId()));
						answerService.addAnswer(answer);
					}
				}
			}
		}catch(Exception e){
			System.out.println(e.toString());
			return result.setErrorMessage("解析JSON失败！").toString();
		}
		return result.setSuccessMessage("提交答案成功！").toString();
	}
	
	public Long getOptionenId(String content,Long questionId){
		Optionen optionen = new Optionen();
		optionen.setQuestionId(questionId);
		if(content.equals("a")){
			optionen.setSigns(Dictionary.Optionen_SIGNS_A);
		}else if(content.equals("b")){
			optionen.setSigns(Dictionary.Optionen_SIGNS_B);
		}else if(content.equals("c")){
			optionen.setSigns(Dictionary.Optionen_SIGNS_C);
		}else if(content.equals("d")){
			optionen.setSigns(Dictionary.Optionen_SIGNS_D);
		}
		return optionenService.findOptionen(optionen).getId();
	}
}
